//******************************************************************/
//版本说明:
//V0.1		2017-03-30	11:00	yshao	复制自SL909_G01_X32
//V2.1		2019-01-31	11:00	yshao	调整init_end和comm_en,init_mode
//V3.0		2019-05-20	23:00	yshao	复制自LL901_G04_X04_11
//V3.0		2019-07-16	23:00	yshao	复制自LL902_G01_X01_12，用于AGM FPGA
//******************************************************************/
//******************************************************************/
//			   全局参数定义
//******************************************************************/
//仿真模式
//`define	SIMULATION	1
//`timescale	1ps/1ps

//FPGA程序模式
//define	APP_MODE	1
//`define	TEST_MODE	2
`define		BOOT_MODE	3

//******************************************************************/
//			   FPGA顶层
//******************************************************************/
module LL902_T01(
		input	wire		sclkin,
		input	wire		key_in,
		input	wire		s_pullup,
		output	wire		led_g,
		
		input	wire	[3:0]	port_ain,		//底层模式，关闭输出

//		input	wire		JTCK,
//		input	wire		JTDI,
//		input	wire		JTMS,
//		output	wire		JTDO,

		inout	tri		flash_SO,
		inout	tri		flash_SI,
		output	wire		flash_SCK,
		output	wire		flash_CS_n,

		output	wire		sa_csn,
		output	wire		sa_cke,
		output	wire		sa_clk,
		output	wire	[2:0]	sa_cnt,
		output	wire	[3:0]	sa_dqm,
		output	wire	[10:0]	sa_addr,
		output	wire	[1:0]	sa_bank,
		inout	wire	[31:0]	sa_data,

		input	wire		mcu_dmx_tx,		//uart2_sync,
		output	wire		mcu_dmx_rx,		//uart3_sync,

		input	wire		mcu_fpga_ctrl,		//mcu到fpga的模式控制信号

		input	wire		mcu_spi_fpga,		//spi工作在通讯模式
		input	wire		mcu_spi_clk,
		input	wire		mcu_spi_mosi,
		output	wire		mcu_spi_miso,

		output	wire		spi2_cs,
		output	wire		spi2_clk,
		output	wire		spi2_mosi,
								
		output	wire		sd_clk,
//		inout	tri		sd_cmd_sdi,		//SD
//		input	wire		sd_dat0_sdo,		//SD
//		input	wire		sd_dat1,		//SD
//		input	wire		sd_dat2,		//SD
//		inout	wire		sd_dat3_cs,		//SD
//		input	wire		sd_cd,			//SD
//		input	wire		sd_wp,			//SD
		output	wire		sd_cmd_sdi,
		output	wire		sd_dat0_sdo,		//JTDO
		output	wire		sd_dat1,
//		output	wire		sd_dat2,		//调试
//		output	wire		sd_dat3_cs,		//调试
//		output	wire		sd_cd,			//调试
		input	wire		sd_dat2,		//JTDI
		input	wire		sd_dat3_cs,		//JTMS
		input	wire		sd_cd,			//JTCK
		output	wire		sd_wp,

		output	wire		phy_rst,
		output	wire		phy_mdc,
		inout	tri		phy_mdio,

		input	wire		gp0_rxc,
		input	wire		gp0_rxdv,
		input	wire	[3:0]	gp0_rxd,
		output	wire		gp0_txc,
		output	wire		gp0_txen,
		output	wire	[3:0]	gp0_txd,
		
		input	wire		gp1_rxc,
		input	wire		gp1_rxdv,
		input	wire	[3:0]	gp1_rxd,
		output	wire		gp1_txc,
		output	wire		gp1_txen,
		output	wire	[3:0]	gp1_txd,

		output	wire	[7:0]	port_da,		//底层模式，关闭输出
		output	wire	[7:0]	port_le,		//底层模式，关闭输出
		input	wire	[3:0]	port_ex,		//底层模式，关闭输出
		input	wire		port_in,		
		
		output	wire	[1:0]	tout
		);

//****************************************************************
//		内部信号
//****************************************************************
wire		resetb, oclk, sclk;
wire	[20:0]	time_us, sync_us;

//PHY设置总线相关信号
wire		init_mode, set_d_ok, ext_d_ok;
wire	[31:0]	set_addr;
wire	[7:0]	set_data, rd_data;

//MCU设置总线相关信号
wire		mcu_set_d_ok, mcu_ext_d_ok;
wire	[31:0]	mcu_set_addr;
wire	[7:0]	mcu_set_data, mcu_rd_data;
wire	[7:0]	mcu_rd_hub;


//显示数据相关
reg		vs, ds, h_start;
reg	[7:0]	data;
reg	[10:0]	h_num;

wire		input_L9, input_artnet;
wire		vs_a, ds_a, h_start_a, vs_sd, ds_sd, h_start_sd;
wire	[7:0]	data_a, data_sd;
wire	[10:0]	h_num_a, h_num_sd;

wire		l2048_mode, artnet_mode;

wire	[15:0]	state;
wire	[1:0]	color_restore_type;
wire	[7:0]	testmode;
wire	[8:0]	cascade_light;

wire		black;

reg	[5:0]	count_34ms;
reg		vs_34ms;

//输出模块接口信号
wire		vs_out;
wire		mem_read_req, mem_read_last, mem_read_ack, mem_data_sync;
wire	[11:0]	mem_h_addr, mem_l_addr;
wire	[31:0]	mem_read_data;	

//输出控制信号
wire		out_sync;
wire 	[15:0]	out_data, out_data_n;
wire		clk_out, clk_out_n;

//DMX输出信号
wire	[1:0]	dmx_mode;
wire		dmx_send_flag;
wire	[15:0]	out_data_dmx;
wire	[5:0]	device_port;
wire		mcu_dmx_ten;

//HUB信号
wire	[7:0]	port_tt;

//SD播放信号
wire		sd_play_en, mcu_play_en, mcu_config_en, force_dis_en, force_send_play;

wire		sd_valid, sd_play_clk, sd_play_cmd_out, sd_play_cmd_oe;
wire	[3:0]	sd_play_data, sd_dat_mcu;
reg	[3:0]	sd_dat_dis;

wire		f_start, f_work, v_work, sd_play_flag;
wire	[40:0]	f_addr;
wire	[23:0]	f_size;
wire	[10:0]	h_size;
wire	[12:0]	l_size;

reg		local_dis_flag, mcu_play_flag;

//反馈网口信号
wire		send_flag_dis, pre_flag_dis;
wire	[7:0]	send_data_dis, sd_tout, hub_tout;

//MCU接口信号
wire		mcu_init_flag;
wire	[7:0]	port_a;
wire		flash_CS_n_fpga, flash_SCK_fpga, flash_SI_fpga;
wire		flash_CS_n_arm, flash_SCK_arm, flash_SI_arm;

wire		mcu_init;

//**********调试用信号**********
wire	[31:0]	a1_tout, out_tout, sa_tout, sd_f_tout, mcu_tout;

//******************************************************************/
//			   参数定义
//******************************************************************/
//程序版本信息
parameter	MAIN_FUNCTION	=  "L";		//ASCII "S"  
parameter	SUB_FUNCTION	=  "L";		//ASCII "L"  
parameter	MAIN_SOLUTION	=  9;		//"9"        
parameter	SUB_SOLUTION	=  2;		//"09"       
parameter	APPLICATION_TYPE=  "T";		//ASCII "G"  
parameter	MAIN_VERSION	=  8'd1;	//"03"       
parameter	SUB_VERSION	=  8'd1;	//"X01"
parameter	MINI_VERSION	=  8'd4;	//" "  

defparam	comm_top.sys_comm.MAIN_FUNCTION		=MAIN_FUNCTION;
defparam	comm_top.sys_comm.SUB_FUNCTION		=SUB_FUNCTION;
defparam	comm_top.sys_comm.MAIN_SOLUTION		=MAIN_SOLUTION;
defparam	comm_top.sys_comm.SUB_SOLUTION		=SUB_SOLUTION;
defparam	comm_top.sys_comm.APPLICATION_TYPE	=APPLICATION_TYPE;
defparam	comm_top.sys_comm.MAIN_VERSION		=MAIN_VERSION;
defparam	comm_top.sys_comm.SUB_VERSION		=SUB_VERSION;
defparam	comm_top.sys_comm.MINI_VERSION		=MINI_VERSION;

//模块参数设置
defparam	comm_top.phy_comm.state_ctrl.main_function	=MAIN_FUNCTION;
defparam	comm_top.phy_comm.state_ctrl.sub_function	=SUB_FUNCTION;
defparam	comm_top.phy_comm.state_ctrl.main_solution	=MAIN_SOLUTION;
defparam	comm_top.phy_comm.state_ctrl.sub_solution	=SUB_SOLUTION;
defparam	comm_top.phy_comm.state_ctrl.application_type	=APPLICATION_TYPE;
defparam	comm_top.phy_comm.state_ctrl.main_version	=MAIN_VERSION;
defparam	comm_top.phy_comm.state_ctrl.sub_version	=SUB_VERSION;
defparam	comm_top.phy_comm.state_ctrl.mini_version	=MINI_VERSION;

defparam        comm_top.mcu_comm.main_function		=	MAIN_FUNCTION;
defparam        comm_top.mcu_comm.sub_function		=	SUB_FUNCTION;
defparam        comm_top.mcu_comm.main_solution		=	MAIN_SOLUTION;
defparam        comm_top.mcu_comm.sub_solution		=	SUB_SOLUTION;
defparam        comm_top.mcu_comm.application_type	=	APPLICATION_TYPE;
defparam        comm_top.mcu_comm.main_version		=	MAIN_VERSION;
defparam        comm_top.mcu_comm.sub_version		=	SUB_VERSION;
defparam        comm_top.mcu_comm.mini_version		=	MINI_VERSION;

parameter	START_IDLE = 2'b00;
parameter	START_DELAY = 2'b01;
parameter	START_OK = 2'b10;

//**************************************************************
//			通讯模块
//**************************************************************
a1_comm_top comm_top(
		//时钟，按键，Led
		.sclkin(sclkin),
		.key_in(key_in),
		.led_g(led_g),

		//输出复位和时钟
		.resetb(resetb),
		.sclk(sclk),
		.oclk(oclk),
		.sa_clk(sa_clk),
		.clk_25M(clk_25M),

		.JTCK(JTCK),
		.JTDI(JTDI),
		.JTMS(JTMS),
		.JTDO(JTDO),
		.mcu_init(mcu_init),

		//Flash接口
		.flash_CS_n(flash_CS_n),//_fpga),
		.flash_SCK(flash_SCK),//_fpga),
		.flash_SI(flash_SI),//_fpga),
		.flash_SO(flash_SO),//),

		//MCU通讯接口
		.mcu_fpga_ctrl(mcu_fpga_ctrl),
		
		.mcu_spi_fpga(mcu_spi_fpga),
		.mcu_spi_clk(mcu_spi_clk),
		.mcu_spi_mosi(mcu_spi_mosi),
		.mcu_spi_miso(mcu_spi_miso),

		.spi2_cs(spi2_cs),
		.spi2_clk(spi2_clk),
		.spi2_mosi(spi2_mosi),

		//PHY通讯接口
		.phy_rst(phy_rst),
		.phy_mdc(phy_mdc),
		.phy_mdio(phy_mdio),

		.gp0_rxc(gp0_rxc),
		.gp0_rxdv(gp0_rxdv),
		.gp0_rxd(gp0_rxd),
		.gp0_txc(gp0_txc),
		.gp0_txen(gp0_txen),
		.gp0_txd(gp0_txd),

		.gp1_rxc(gp1_rxc),
		.gp1_rxdv(gp1_rxdv),
		.gp1_rxd(gp1_rxd),
		.gp1_txc(gp1_txc),
		.gp1_txen(gp1_txen),
		.gp1_txd(gp1_txd),

		//SD卡通讯接口
//		.sd_clk(sd_clk),
//		.sd_cmd_sdi(sd_cmd_sdi),
//		.sd_dat0_sdo(sd_dat0_sdo),
//		.sd_dat1(sd_dat1),
//		.sd_dat2(sd_dat2),
//		.sd_dat3_cs(sd_dat3_cs),
		.sd_cd(1'b0),
//		.sd_wp(sd_wp),

		//分频时钟
		.time_us(time_us),
		.sync_us(sync_us),

		//MCU总线接口
		.mcu_set_d_ok(mcu_set_d_ok),
		.mcu_ext_d_ok(mcu_ext_d_ok),
		.mcu_set_addr(mcu_set_addr),
		.mcu_set_data(mcu_set_data),
		.mcu_rd_data(mcu_rd_data),

		//PHY总线接口
		.init_mode(init_mode),
		.set_d_ok(set_d_ok),
		.ext_d_ok(ext_d_ok),
		.set_addr(set_addr),
		.set_data(set_data),
		.rd_data(rd_data),

		//显示数据接口
		.vs(vs_a),
		.ds(ds_a),
		.data(data_a),
		.h_start(h_start_a),
		.h_num(h_num_a),
		.l2048_mode(l2048_mode),
		.artnet_mode(artnet_mode),

		.state(state),
		.color_restore_type(color_restore_type),
		.testmode(testmode),
		.cascade_light(cascade_light),

		.black(black),

		//背板控制接口
		.device_port(device_port),
		.fpga_dmx_send(dmx_send_flag),
		.mcu_dmx_ten(mcu_dmx_ten),
		
		//SD卡播放指示
		.input_L9(input_L9),
		.input_artnet(input_artnet),
		.sd_valid(sd_valid),
		.sd_play_flag(sd_play_flag),
		.local_dis_flag(local_dis_flag),

		//SD卡播放操作接口
		.sd_play_clk(sd_play_clk),
		.sd_play_cmd_out(sd_play_cmd_out),
		.sd_play_cmd_oe(sd_play_cmd_oe),
		.sd_play_data(sd_play_data),
		//SD发送数据包接口
		.force_send_play(force_send_play),
		.send_flag_dis(send_flag_dis),
		.pre_flag_dis(pre_flag_dis),
		.send_data_dis(send_data_dis),

		//调试接口
		.tout(a1_tout)   
		);

assign	mcu_rd_data = mcu_rd_hub;
assign	sd_play_flag = v_work | f_work;

//**************************************************************
//			MCU模块
//**************************************************************
/*lcd mcu_top1(
		.clk(sclkin),
		.rst(~init_mode),
		
		.led(),
		
		//jtag接口
		.JTCK(JTCK),
		.JTDI(JTDI),
		.JTMS(JTMS),
		.JTDO(JTDO),
	 
		//flash接口
		.FLASH_CS_n(flash_CS_n_arm),
		.FLASH_SCK(flash_SCK_arm),
		.FLASH_IO0_SI(flash_SI_arm),
		.FLASH_IO1_SO(flash_SO),
		
		.mcu_init_flag(mcu_init_flag),

		.port_a(port_a),

		.tout(mcu_tout)
		);

always @( * )
	if ((init_mode == 0) && (mcu_init_flag == 1)) begin
		flash_CS_n <= flash_CS_n_arm;
		flash_SCK <= flash_SCK_arm;
		flash_SI <= flash_SI_arm;
		end
	else
		begin
		flash_CS_n <= flash_CS_n_fpga;
		flash_SCK <= flash_SCK_fpga;
		flash_SI <= flash_SI_fpga;
		end


always @( * ) begin
	flash_CS_n <= flash_CS_n_fpga;
	flash_SCK <= flash_SCK_fpga;
	flash_SI <= flash_SI_fpga;
	end

assign	port_a = 0;
assign	JTDO = 0;
*/
//**************************************************************
//         	空闲端口
//************************************************************** 
assign	sa_csn = 1'b1;
assign	sa_cke = 1'b1;
assign	sa_cnt = 3'b111;
assign	sa_dqm = 4'h0;
assign	sa_addr = 11'h0;
assign	sa_bank = 2'h0;

//************************************************************/
//		MCU JTAG接口
//************************************************************/                    
assign	JTDI = sd_dat2;
assign	sd_dat0_sdo = JTDO;
assign	JTCK = sd_cd;
assign	JTMS = sd_dat3_cs;

//assign	sd_dat2 = mcu_spi_fpga;
//assign	sd_dat0_sdo = mcu_spi_clk;
//assign	sd_cd = mcu_spi_mosi;
//assign	sd_dat3_cs = mcu_spi_miso;
//************************************************************/
//		调试接口
//************************************************************/                    
assign	port_da = a1_tout[7:0];
assign	port_le = {JTDI, JTMS, JTCK, JTDO, a1_tout[11:8]};

assign	sd_dat1 = 0;//flash_CS_n;
assign	sd_wp = 0;//flash_SCK;
assign	sd_clk = 0;//flash_SI;
assign	sd_cmd_sdi = 0;//flash_SO;

//assign	port_ain[0] = flash_CS_n;
//assign	port_ain[1] = flash_SCK;
//assign	port_ain[2] = flash_SI;
//assign	port_ain[3] = flash_SO;

assign	tout[0] = init_mode;
assign	tout[1] = mcu_init_flag;

endmodule
